package com.ruoyi.web.controller.subjectlow;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.YcSubjectLow;
import com.ruoyi.system.service.IYcSubjectLowService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 详细学科Controller
 *
 * @author 杨琛
 * @date 2020-11-21
 */
@Controller
@RequestMapping("/system/low")
public class YcSubjectLowController extends BaseController {
    private String prefix = "system/low";

    /**
     * 定义一个成员变量id  用来接受additive方法的传递过来的次要学科id
     */
    private Long id;

    @Autowired
    private IYcSubjectLowService ycSubjectLowService;

    @RequiresPermissions("system:low:view")
    @GetMapping()
    public String low() {
        return prefix + "/low";
    }

    /**
     * 查询详细学科列表
     */
    @RequiresPermissions("system:low:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YcSubjectLow ycSubjectLow) {
        startPage();
        //添加查询条件,点击那个次要学科就显示只和次要学科有关的详细学科
        ycSubjectLow.setRelationId(this.id);
        List<YcSubjectLow> list = ycSubjectLowService.selectYcSubjectLowList(ycSubjectLow);
        return getDataTable(list);
    }

    /**
     * 导出详细学科列表
     */
    @RequiresPermissions("system:low:export")
    @Log(title = "详细学科", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YcSubjectLow ycSubjectLow) {
        List<YcSubjectLow> list = ycSubjectLowService.selectYcSubjectLowList(ycSubjectLow);
        ExcelUtil<YcSubjectLow> util = new ExcelUtil<YcSubjectLow>(YcSubjectLow.class);
        return util.exportExcel(list, "low");
    }

    /**
     * 新增详细学科
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存详细学科
     */
    @RequiresPermissions("system:low:add")
    @Log(title = "详细学科", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YcSubjectLow ycSubjectLow) {
        //添加详细学科的时候将次要学科的id添加上去
        ycSubjectLow.setRelationId(this.id);
        return toAjax(ycSubjectLowService.insertYcSubjectLow(ycSubjectLow));
    }

    /**
     * 修改详细学科
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        YcSubjectLow ycSubjectLow = ycSubjectLowService.selectYcSubjectLowById(id);
        mmap.put("ycSubjectLow", ycSubjectLow);
        return prefix + "/edit";
    }

    /**
     * 修改保存详细学科
     */
    @RequiresPermissions("system:low:edit")
    @Log(title = "详细学科", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YcSubjectLow ycSubjectLow) {
        return toAjax(ycSubjectLowService.updateYcSubjectLow(ycSubjectLow));
    }

    /**
     * 删除详细学科
     */
    @RequiresPermissions("system:low:remove")
    @Log(title = "详细学科", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ycSubjectLowService.deleteYcSubjectLowByIds(ids));
    }


    /**
     * 将次要学科的id传递给次要学科下面的详细学科
     */
    @RequiresPermissions("additive:stimulus:minor")
    @GetMapping("/additive/{id}")
    public String additive(@PathVariable("id") Long id) {
        //id为前台传过来的值,this.id为该类的成员变量用来存储
        this.id=id;
        return prefix + "/low";
    }
}
